<?php
// +----------------------------------------------------------------------
// | Author: wangxj <wangxj@bravesoft.co.jp>
// +----------------------------------------------------------------------
namespace Think;
use Think\Controller;

class ControllerEx extends Controller {

    private $dbName;
    public  $dbTable;

    Public function base_init($dbName)
    {
        $this->dbName  = $dbName;
        if($dbName){
            $this->dbTable = D($dbName);
        }
        
        $ses = session();
        if(empty($ses)){   
           $this->redirect('Home/Login/index');   
        }
    }
	
    public function base_list()  {
        $this->assign('loginedName',$_SESSION['name']);//header显示名字
		$this->assign('utype',$_SESSION['utype']);
		
		$this->assign('errorMsg',$_SESSION['errorMsg']);      
        $this->assign('errorCode',$_SESSION['errorCode']);//显示成功和失败提示信息
        
		$this->display();
		
        $_SESSION['errorMsg'] = "";
        $_SESSION['errorCode'] = "";     
    }


    public function base_add($data,$redirectId='') {
        $this->assign('loginedName',$_SESSION['name']);//header显示名字
		$this->assign('utype',$_SESSION['utype']);
      
        if($this->base_isSubmit())
        {
            $this->dbTable->create($data);
			//$this->dbTable->add();
			//echo $this->dbTable->getLastSql(); echo ACTION_NAME;die(); 
            if($this->dbTable->add())
            {
                $_SESSION['errorMsg'] ="追加成功";
                $_SESSION['errorCode'] = '1';//提示成功信息和跳转
            }else{
                $_SESSION['errorCode'] = '0';//提示失败信息和跳转
                $_SESSION['errorMsg'] = "追加失败";
            }
            if ($redirectId == '') {
                $this->redirect('index');
            }else{
                $this->redirect('index',array('id'=>$redirectId));
            }
        }else{
            $this->display();
        } 
    }

    public function base_edit($id,$data,$redirectId='') {
        $this->assign('loginedName',$_SESSION['name']);//header显示名字
		$this->assign('utype',$_SESSION['utype']);
		
		$data['id'] = $id;
       if($this->base_isSubmit()){
            $this->dbTable->create($data);
            if($this->dbTable->save() === false){
                $_SESSION['errorCode'] = '0';//提示失败信息和跳转
                $_SESSION['errorMsg'] = "修改失败";
            }else{
                $_SESSION['errorMsg'] ="修改成功";
                $_SESSION['errorCode'] = '1';//提示成功信息和跳转
            }
            if ($redirectId == '') {
                $this->redirect('index');
            }else{
                $this->redirect('index',array('id'=>$redirectId));
            }
        }else{
			$condition['id'] = $id;
			$result=$this->dbTable->where($condition)->find();
			$this->assign('data',$result);

            $this->display();
        }
    }
	
	public function base_view() {
        $this->assign('loginedName',$_SESSION['name']);//header显示名字
		$this->assign('utype',$_SESSION['utype']);
		
  		$this->assign('errorMsg',$_SESSION['errorMsg']);      
        $this->assign('errorCode',$_SESSION['errorCode']);//显示成功和失败提示信息
		
	    $this->display();
		
	    $_SESSION['errorMsg'] = "";
        $_SESSION['errorCode'] = ""; 
     
    }
	public function base_delete($id,$redirectId='') {
        
		$condition['id']=I('id');

        if($this->dbTable->where($condition)->delete()){
            $rdata['code'] = 1;
            $rdata['message'] = "删除成功";
            
         }else{
            $rdata['code'] = 0;
            $rdata['message'] = "删除失败";
        }
    }


	public function base_isSubmit(){
		if(isset($_POST['addsub']) || isset($_POST['editsub']) || isset($_POST['viewsub']))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
}
